/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.report.service.impl;


import com.koron.common.domain.DataBean;
import com.koron.common.web.Util;
import com.koron.report.bean.LocalHazarsSourceAnalysis;
import com.koron.report.mapper.DataAnalysisMapper;
import com.koron.report.service.api.DangerAnalysisService;
import com.koron.report.service.api.LocalHazradSourceAnalysisService;
import com.koron.report.util.BusinessCode;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.service.api.CorpManagementService;
import com.koron.risk.service.api.GridNodeService;
import com.koron.risk.service.api.HazardSourceDistributeService;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class LocalHazradSourceAnalysisServiceImpl implements LocalHazradSourceAnalysisService {

    @Autowired
    private DangerAnalysisService dangerAnalysisService;

    @Autowired
    private Util util;

    @Autowired
    private CorpManagementService corpManagementService;

    @Autowired
    private HazardSourceDistributeService distributeService;

    @Autowired
    private GridNodeService nodeService;

    @Override
    public MessageBean<DataBean<LocalHazarsSourceAnalysis>> getLocalHazardSourceAnalysisListByLevel(Integer orgLevel, String orgCode, Date startTime, Date endTime, Integer page, Integer pageSize) {
        MessageBean<DataBean<LocalHazarsSourceAnalysis>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            Map<String,String> map=dangerAnalysisService.getAnalysisDateMap();
            List<LocalHazarsSourceAnalysis> analysisList=new ArrayList<>();
            switch (orgLevel){
                case 0:
                    //是集团，返回所有水司的数据
                   msg=getLocalHazardSourceAnalysisListByCorp(startTime,endTime,page,pageSize);
                   return msg;
                case 1:
                    //水司数据
                    //返回当前水司的数据
                    CorpBean corpBean=corpManagementService.getOne(orgCode).getData();
                    List<String> corpCodeList=new ArrayList<>();
                    corpCodeList.add(corpBean.getCorpCode());
                    LocalHazarsSourceAnalysis sourceAnalysis1=factory.getMapper(DataAnalysisMapper.class).queryCountRiskHazardSource(startTime,endTime,corpCodeList);
                    LocalHazarsSourceAnalysis sourceAnalysis2=factory.getMapper(DataAnalysisMapper.class).queryCountHazrad(map.get("majorHazardSource"),startTime,endTime,corpCodeList);
                    if (null==sourceAnalysis1)
                        sourceAnalysis1=new LocalHazarsSourceAnalysis();
                    sourceAnalysis1.setMajorHazardSource(sourceAnalysis2.getMajorHazardSource());
                    sourceAnalysis1.setDepartmentCode(corpBean.getCorpCode());
                    sourceAnalysis1.setDepartmentName(corpBean.getCorpName());
                    analysisList.add(sourceAnalysis1);
                    break;
                default:
                    break;
            }
            if(analysisList!=null&&analysisList.size()>0){
                Integer totalNumber=analysisList.size();
                pageSize=pageSize==null||pageSize<=0?totalNumber:pageSize;
                Integer start=0,end=15,totalPage=1;
                if(page!=null&&page>1){
                    if(pageSize==null||pageSize<=0){
                        pageSize=15;
                    }
                    start=(page-1)*pageSize;
                    end=start+pageSize;
                }
                if (end>totalNumber){
                    end=totalNumber;
                    if (start>end){
                        start=end;
                    }
                    totalPage = totalNumber / pageSize + 1;
                }
                DataBean<LocalHazarsSourceAnalysis> dataBean=new DataBean<>();
                dataBean.setPageSize(pageSize);
                dataBean.setPageNo(page);
                dataBean.setTotalPage(totalPage);
                dataBean.setTotalNumber(totalNumber);
                dataBean.setList(analysisList.subList(start,end));
                msg.setData(dataBean);
            }
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            return msg;
        }catch (Exception e){
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            return msg;
        }
    }

    @Override
    public MessageBean<DataBean<LocalHazarsSourceAnalysis>> getLocalHazardSourceAnalysisListByCorp(Date startTime, Date endTime, Integer page, Integer pageSize) {
      MessageBean<DataBean<LocalHazarsSourceAnalysis>> msg=new MessageBean<>();
      try(SessionFactory factory=new SessionFactory()){
          Map<String,String> map=dangerAnalysisService.getAnalysisDateMap();
          List<LocalHazarsSourceAnalysis> analysisList=new ArrayList<>();
          List<CorpBean> corpBeanList=corpManagementService.corpBeanList();
          if(null!=corpBeanList&&corpBeanList.size()>0){
              for(CorpBean corpBean:corpBeanList){
                  List<String> corpCodeList=new ArrayList<>();
                  corpCodeList.add(corpBean.getCorpCode());
                  LocalHazarsSourceAnalysis sourceAnalysis5=factory.getMapper(DataAnalysisMapper.class).queryCountRiskHazardSource(startTime,endTime,corpCodeList);
                  LocalHazarsSourceAnalysis sourceAnalysis6=factory.getMapper(DataAnalysisMapper.class).queryCountHazrad(map.get("majorHazardSource"),startTime,endTime,corpCodeList);
                  if (null==sourceAnalysis5)
                      sourceAnalysis5=new LocalHazarsSourceAnalysis();
                  sourceAnalysis5.setMajorHazardSource(sourceAnalysis6.getMajorHazardSource());
                  sourceAnalysis5.setDepartmentCode(corpBean.getCorpCode());
                  sourceAnalysis5.setDepartmentName(corpBean.getCorpName());
                  analysisList.add(sourceAnalysis5);
              }
          }
          if(analysisList!=null&&analysisList.size()>0){
              Integer totalNumber=analysisList.size();
              pageSize=pageSize==null||pageSize<=0?totalNumber:pageSize;
              Integer start=0,end=15,totalPage=1;
              if(page!=null&&page>1){
                  if(pageSize==null||pageSize<=0){
                      pageSize=15;
                  }
                  start=(page-1)*pageSize;
                  end=start+pageSize;
              }
              if (end>totalNumber){
                  end=totalNumber;
                  if (start>end){
                      start=end;
                  }
                  totalPage = totalNumber / pageSize + 1;
              }
              DataBean<LocalHazarsSourceAnalysis> dataBean=new DataBean<>();
              dataBean.setPageSize(pageSize);
              dataBean.setPageNo(page);
              dataBean.setTotalPage(totalPage);
              dataBean.setTotalNumber(totalNumber);
              dataBean.setList(analysisList.subList(start,end));
              msg.setData(dataBean);
          }
          msg.setCode(BusinessCode.SUCCESS.getCode());
          msg.setDescription(BusinessCode.SUCCESS.getDescription());
          return msg;
      }catch (Exception e){
          msg.setDescription(BusinessCode.EXCEPTION.getDescription());
          msg.setCode(BusinessCode.EXCEPTION.getCode());
          return msg;
      }
    }
}
